#include <docker.h>
DockerContainer::~DockerContainer(){
cout<<"容器销毁"<<endl;
}
DockerContainer::DockerContainer(){
    // 根挂载卷
    const char* root=getenv("DOCKER_ROOT_VOLUME");
    // 判断 根挂载卷是否为空
    if(root ==nullptr){
        cout<<"根挂载卷为空 将设置默认 /opt/app_date"<<endl;
        this->rootVolume="/opt/app_date";
    }else{
        this->rootVolume=string(root);
        cout<<"根挂载卷设置为: "<<this->rootVolume<<endl;
    }

this->ymlPath="/mnt/net/work/make/util-docker/main/doc/service.yml";
this->networks();
this->services();
stringWriteFile(this->ymlPath.c_str(),this->ymlFile);
}

/**
* @brief 网络配置
* @return 成功返回 0，失败返回 -1
*/
int DockerContainer::networks(){
    // 网络昵称设置
    const char* networks=getenv("YML_NETWORKS");
    if(networks==nullptr){
        cout<<"网络昵称为设置 将设置默认 my_networks"<<endl;
        this->yml_networks="my_networks";
    }else{
        cout<<"网络昵称设置为: "<<networks<<endl;
        this->yml_networks=string(networks);
    }
    // 网络配置子网配置
    const char* subnet_ip=getenv("YML_SUBNET_IP");
    if(subnet_ip==nullptr){
        cout<<"网络配置子网配置 将设置默认 192.168.100.1/24"<<endl;
        this->yml_subnet="192.168.100.1/24";

    }else{
        this->yml_subnet=string(subnet_ip);
        cout<<"网络配置子网配置设置为: "<<this->yml_subnet<<endl;
    }
    // 网络配置
    this->ymlFile="networks: #网络配置 \n   ";
    this->ymlFile+=this->yml_networks+": #网络昵称 \n     ";
    this->ymlFile+="driver: bridge #网络驱动 \n     ";
    this->ymlFile+="ipam: #网络配置 \n       ";
    this->ymlFile+="config: #网络配置 \n         ";
    this->ymlFile+="- subnet: "+this->yml_subnet+" #网络配置 \n";
    this->ymlFile+="services: # 服务定义\n  ";
    return 0;
}
/**
* @brief 网络配置
* @param ip 网络配置
* @return 成功返回 0，失败返回 -1
*/
string DockerContainer::getIp(string ip){
    // 去除 ip 中的掩码
    string result;
    size_t pos = this->yml_subnet.find('/');
    // 判断 掩码是否存在 如果不存在 则返回 nullptr
    if (pos == string::npos)return nullptr;
    result = this->yml_subnet.substr(0, pos-1)+ip;
    return result;
}
/**
* @brief 服务配置
* @return 成功返回 0，失败返回 -1
*/
int DockerContainer::services(){
    bool isNginx=false;
    // 获取系统变量 VESSEL 启动容器对象
    const char *ve=getenv("DOCKER_VESSEL");
    // 判断 系统变量是否为空
    if(ve ==nullptr)return -1;
    const char* delimiter = ",";
    string strvs = string(ve);
    vector<string> vvs = string_split(strvs,delimiter);
    for(string v : vvs){
        const char *cv=v.c_str();
        // mysql 服务启动
        if (strcmp(cv, "mysql") == 0){
            this->mysqlServices(v);
            // 跳过
            continue;
        }
        // redis 服务启动
        if (strcmp(cv, "redis") == 0){
            this->redisServices(v);
            // 跳过
            continue;
        }
        // nexus3 服务启动
        if (strcmp(cv, "nexus3") == 0){
            this->nexus3Services(v);
            // 跳过
            continue;
        }
        // alist服务配置
        if (strcmp(cv, "alist") == 0){
            this->alistServices(v);
            // 跳过
            continue;
        }
        // aria2服务配置
        if (strcmp(cv, "aria2") == 0){
            this->aria2Services(v);
            // 跳过
            continue;
        }
        // video服务配置
        if (strcmp(cv, "video") == 0){
            this->videoServices(v);
            // 跳过
            continue;
        }
        // nginx proxy 服务配置
        if (strcmp(cv, "nginx_proxy") == 0){
            isNginx=true;
            // 跳过
            continue;
        }
    }
    // nginx proxy 服务配置
    if(isNginx){
       this->nginxProxyServices("nginx_proxy");
    }

    return 0;
}
/**
* @brief 添加服务
* @param services 服务
* @return 成功返回 0，失败返回 -1
*/
int DockerContainer::addServices(VesselServices services){
    this->ymlFile+=services.name+": # 服务名称 \n      ";
    this->ymlFile+="# 启动镜像 \n      ";
    this->ymlFile+="image: "+services.image+" \n      ";
    this->ymlFile+="container_name: "+services.name+" # 容器名称 \n      ";
    this->ymlFile+="restart: unless-stopped # 容器已经停止，否则在任何情况下都会尝试重启容器 \n      ";
    this->ymlFile+="networks: # 网络配置 \n        ";
    this->ymlFile+=this->yml_networks+": # 网络名称     \n          ";
    this->ymlFile+="ipv4_address: "+services.ip+" # 容器 ip     \n      ";
    if(!services.command.empty()){
        this->ymlFile+="# 容器启动命令 \n      ";
        this->ymlFile+="command: \""+services.command+"\" \n      ";
    }

    // 判断 容器 端口 是否为空
    if(services.ports.size()>0){
        this->ymlFile+="# 容器端口 \n      ";
        this->ymlFile+="ports: # 容器端口 \n       ";
        // 遍历 容器 环境变量
        for(int i=0;i<services.ports.size();i++){
            this->ymlFile+="  - '"+services.ports[i]+"' \n       ";
        }
    }

    // 判断 容器 环境变量 是否为空
    if(services.envs.size()>0){
        this->ymlFile+="# 容器变量 \n      ";
        this->ymlFile+="environment: \n       ";
        // 遍历 容器 环境变量
        for(int i=0;i<services.envs.size();i++){
            this->ymlFile+=" - \""+services.envs[i]+"\" \n       ";
        }
    }

    // 判断 容器 挂载卷 是否为空
    if(services.volumes.size()>0){
        this->ymlFile+="# 容器挂载卷 \n      ";
        this->ymlFile+="volumes: \n       ";
        // 遍历 容器 挂载卷
        for(int i=0;i<services.volumes.size();i++){
            this->ymlFile+="  - \""+services.volumes[i]+"\" \n       ";
        }
    }

    this->ymlFile+="\n  ";
    return 0;
}